package com.itheima.info.manager.system.dao;

import com.itheima.info.manager.system.domain.Student;

import java.util.ArrayList;

/*
学生的 数据操作类
集合

增
删
改
查


 */
public class OtherStudentDao implements BaseStudentDao{
    //创建 数组
    private static ArrayList<Student> stus = new ArrayList<Student>();

    static {
        Student student1 = new Student("00001", "鹿晗", 30, "1991");
        Student student2 = new Student("00002", "吴签", 31, "1990");
        Student student3 = new Student("00003", "坤坤", 26, "1995");
        Student student4 = new Student("00004", "滔滔", 28, "1993");
        stus.add(student1);
        stus.add(student2);
        stus.add(student3);
        stus.add(student4);

    }


    //新增  参数  学生对象   返回值  布尔类型
    // 添加到集合中
    public boolean addStudent(Student student) {
        stus.add(student);
        return true;
    }

    //查询所有
    //参数   不要
    //返回值类型  所有的学生  Student[]
    public Student[] findAllStudents() {
       /* Student[] students = new Student[stus.size()];
        for (int i = 0; i < stus.size(); i++) {
            Student student = stus.get(i);
            students[i]=student;
        }*/

        // return students ;
        return stus.toArray(new Student[0]);
    }


    public void deleteStudent(String sid) {
        //根据学号 找索引的方法
        int index = getIndex(sid);
        //从数组中将元素删除  数组名[索引]=null;
        stus.remove(index);

    }

    //根据学号找索引  查
    //参数 String sid  返回值类型 int
    public int getIndex(String sid) {
        for (int i = 0; i < stus.size(); i++) {
            Student student = stus.get(i);
            if (student.getSid().equals(sid)) {
                return i;
            }
        }
        return -1;
    }

    public void updateStudent(String sid, Student student) {
        int index = getIndex(sid);
        stus.set(index, student);
    }
}
